package com.fortune.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.fortune.util.YearMonthDayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fortune.common.paging.PagingData;
import com.fortune.dao.DistributorOrderMgtDao;
import com.fortune.dao.MstoreTransDao;
import com.fortune.dto.in.mStoreTrans.MStoreTransAListDto;
import com.fortune.dto.in.mStoreTrans.MStoreTransAddDetailDto;
import com.fortune.dto.in.mStoreTrans.MStoreTransAddDto;
import com.fortune.dto.in.mStoreTrans.MStoreTransEditDto;
import com.fortune.entity.MUserInfo;
import com.fortune.service.MStoreTransService;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.mStoreTrans.MStoreTransInfo;
import com.fortune.vo.mStoreTrans.MStoreTransInfoDetail;
import com.fortune.vo.mStoreTrans.MstoreTransDetail;
@Service
public class MStoreTransServiceImpl implements MStoreTransService{
	@Autowired
	private MstoreTransDao md;
	@Autowired
	private DistributorOrderMgtDao distributorOrderMgtDao;
	
	@Override
	public PagingData<MStoreTransInfo> pagingData(MUserInfo mUserInfo, MStoreTransAListDto listDto) {
		List<MStoreTransInfo> list = md.list(mUserInfo, listDto);
		for (MStoreTransInfo mStoreTransInfo : list){
			if(mStoreTransInfo.getCreateTime()!= null && mStoreTransInfo.getCreateTime() != ""){
					mStoreTransInfo.setCreateTime(mStoreTransInfo.getCreateTime().substring(0,10));
			}
			if(mStoreTransInfo.getInTime()!= null && mStoreTransInfo.getInTime() != ""){
				mStoreTransInfo.setInTime(mStoreTransInfo.getInTime().substring(0,10));
			}
			if(mStoreTransInfo.getOutTime()!= null && mStoreTransInfo.getOutTime() != ""){
				mStoreTransInfo.setOutTime(mStoreTransInfo.getOutTime().substring(0,10));
			}
		}
		int count = md.count(mUserInfo, listDto);
		PagingData<MStoreTransInfo> pagingData = new PagingData<>(list,count);
		return pagingData;
	}
	@Override
	public MstoreTransDetail mstoreTransDetail(MUserInfo mUserInfo, String id) {
		MstoreTransDetail mstoreTransDetail = md.detail(mUserInfo, id);
		if(mstoreTransDetail.getCreateTime() != "" || mstoreTransDetail.getCreateTime() != null){
			mstoreTransDetail.setCreateTime(mstoreTransDetail.getCreateTime().substring(0,10));
		}
		return mstoreTransDetail;
	}
	@Override
	@Transactional
	public int add(MUserInfo mUserInfo, MStoreTransAddDto addDto) {
		String mId = mUserInfo.getmId();
		String shipStore = addDto.getOutStore();
		for (MStoreTransAddDetailDto addDetailDto : addDto.getList()) {
			int sum = addDetailDto.getNum();
			String id = addDetailDto.getMpid();
			int count = distributorOrderMgtDao.storeCount(mId,id,shipStore);
			int outCount = distributorOrderMgtDao.outCount(mId,id,shipStore);
			int transCount = distributorOrderMgtDao.transCount(mId, id, shipStore);
			if (sum>(count-outCount-transCount)) {
				return 2;
			}
		}
		String uuid = UUIDUtil.get32UUID();
		int add = md.add(uuid, mUserInfo, addDto);
		if (add==1) {
			for (MStoreTransAddDetailDto addDetailDto : addDto.getList()) {
				md.addDetail(uuid, mUserInfo, addDetailDto);
			}
			return add;
		}
		return 0;
	}
	@Override
	@Transactional
	public int edit(MUserInfo mUserInfo, MStoreTransEditDto editDto) {
		int edit =  md.edit(mUserInfo, editDto);
		if (edit==1) {
			int delete = md.deleteDetail(mUserInfo, editDto);
			String uuid = editDto.getId();
			for (MStoreTransAddDetailDto addDetailDto : editDto.getList()) {
				md.addDetail(uuid, mUserInfo, addDetailDto);
			}
			return edit;
		}
		return 0;
	}
	@Override
	public int delete(MUserInfo mUserInfo, String id) {
		int delete = md.delete(mUserInfo, id);
		return delete;
	}
	@Override
	@Transactional
	public int outConfirm(MUserInfo mUserInfo, String id) {
		MstoreTransDetail mstoreTransDetail = md.detail(mUserInfo, id);
		if(mstoreTransDetail == null){
			return 3;
		}
		for(MStoreTransInfoDetail mstoreTransInfoDetail:mstoreTransDetail.getList()){
			String mId = mUserInfo.getmId();
			String outStore = mstoreTransDetail.getOutId();
			String mpid = mstoreTransInfoDetail.getMpid();
			String allStore = outStore;
			Integer num = md.findRemainNum(mId,allStore,mpid);
			if(num == null){
				num = 0;
			}
			int a  =num - mstoreTransInfoDetail.getMpNum();
			if(a < 0){
				return 4;
			}
			int productOut = md.productOut(num,mId, outStore,id,mstoreTransInfoDetail);
			if (productOut!=1) {
				return 2;
			}
		}
		int result = md.outConfirm(mUserInfo, id);
		return result;
	}
	@Override
	@Transactional
	public int inConfirm(MUserInfo mUserInfo, String id) {
		MstoreTransDetail mstoreTransDetail = md.detail(mUserInfo, id);
		for(MStoreTransInfoDetail mstoreTransInfoDetail:mstoreTransDetail.getList()){
			String mId = mUserInfo.getmId();
			String inStore = mstoreTransDetail.getInId();
			String mpid = mstoreTransInfoDetail.getMpid();
			String allStore = inStore;
			Integer num = md.findRemainNum(mId,allStore,mpid);
			if(num == null){
				num = 0;
			}
			int productIn = md.productIn(num,mId,inStore,id,mstoreTransInfoDetail);
			if (productIn!=1) {
				return 2;
			}
		}
		int result = md.inConfirm(mUserInfo, id);
		return result;
	}

}
